package ui.components.core;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;


import application.Application;




/*
 * Base class for the UI library. Simple wrapper around JComponent. Collaborates 
 * with Application and Popup classes to implement custom pop-up components 
 * support.
 */

public class CComponent extends JComponent {
    public CComponent(Application app) {
        super();
        
        if (app != null) {
            this.application = app;
        } else if (Application.application != null) {
            this.application = Application.application;
        } else {
            /*
             * It is expected that we are either given a valid VApplication 
             * handle when constructing a VComponent or that the static
             * global 'application' variable inside VApplication has been
             * initialized by the application.  if neither of these two 
             * are true, we throw an exception. 
             * 
             * TODO: throw an exception
             */ 
            
            /*
             * On second thought, can we continue life with application pointer
             * in the component being null?  just assume that we are not dealing
             * with any popups or the user will manually handle popups
             */
        }
        
        // the main motivation to use a common VComponent base class is to have
        // a common location where we gather mouse-click events so that we can
        // implement pop-up menus in an application: any mouse-click event 
        // triggered anywhere in the application will cause any open pop-up
        // menu's to be closed.
     
        if (this.application != null) {
            VComponentMouseListener mouseListener = new VComponentMouseListener(this);
            this.addMouseListener(mouseListener);
        }
    }
    
    public CComponent() {
        if (Application.application != null) {
            this.application = Application.application;
        } else {
    
        }
     
        VComponentMouseListener mouseListener = new VComponentMouseListener(this);
        this.addMouseListener(mouseListener);
    }
    

    protected Application application = null;
}

class VComponentMouseListener extends MouseAdapter
{
    public VComponentMouseListener(CComponent parent)
    {
        super();
        this.parent = parent;
    }
    
    public void mouseClicked(MouseEvent e)
    {
        // TODO: signal the pop-up list in the application object held in the
        // VComponent 'parent' object. do null check on 'parent'
    }
    
    protected CComponent parent = null;
}